/**
 * 
 * @author  phoenics@126.com
 * @version v.0.1
 * @date  2018年3月18日 下午4:34:14 
 * 
 */
package phoenics.upservice.repository;


import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import phoenics.upservice.entity.Project;
import phoenics.upservice.entity.UserProject;

/**
 *
 */
public interface ProjectRepo extends PagingAndSortingRepository<Project, Long> {  
	 @Query( value = " select p from Project p  where p.projectCategory=?1 and p.projectArea=?2 and p.id<=?3 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project> projectPageDataUp(String projectCategory,String projectArea,long max, Pageable pageable);
	 
	 @Query( value = " select p from Project p  where p.projectCategory=?1 and p.id<=?2 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findByProjectCategoryUp(String projectCategory,long max,Pageable pageable);
	 
	 @Query( value = " select p from Project p  where p.projectArea=?1 and p.id<=?2" )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findByProjectAreaUp(String projectArea,long max, Pageable pageable);
	 
	 @Query( value = " select p from Project p  where  p.id<=?1 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findAllUp(long max, Pageable pageable);
	 
	 @Query( value = " select p from Project p  where  p.id<=?1  and p.user.id=?2 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findAllUp(long max,long userId, Pageable pageable);
	 
	 //////////////////////down
	 @Query( value = " select p from Project p  where p.projectCategory=?1 and p.projectArea=?2 and p.id>?3 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project> projectPageDataDown(String projectCategory,String projectArea,long min, Pageable pageable);
	 
	 @Query( value = " select p from Project p  where p.projectCategory=?1 and p.id>?2 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findByProjectCategoryDown(String projectCategory,long min,Pageable pageable);
	 
	 @Query( value = " select p from Project p  where p.projectArea=?1 and p.id>?2" )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findByProjectAreaDown(String projectArea,long min, Pageable pageable);
	 
	 @Query( value = " select p from Project p  where  p.id>?1 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findAllDown(long min, Pageable pageable);
	 
	 @Query( value = " select p from Project p  where  p.id>?1 and p.user.id=?2 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findAllDown(long min,long userId,  Pageable pageable);
	 
	 @Query( value = " select p from Project p  where exists( select u from User u where p in elements(u.followProjects) and u.id=?1) " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Page<Project>  findFollows(Long userId, Pageable pageable);
	 @Query( value = " select p from Project p  where exists( select u from User u where p in elements(u.followProjects) and u.id=?1) " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 List<Project>  findFollows(Long userId);
	 @Query( value = " select p from Project p  where exists( select u from User u where p in elements(u.followProjects) and u.id=?1)  and p.id=?2" )  
	 Optional<Project>  findFollow(Long userId,Long objectId);
	 
	 //@Query( value = " select p from Project p  where  p.id=?1 " )  
	 @EntityGraph(attributePaths = { "projectPics" })
	 Project  findProjectByid(Long id);
	 
	 @EntityGraph(attributePaths = { "projectPics" })
	 Project  findProjectByIdAndUserId(Long id,Long userId);
	 
	 @Query( value = " select up from UserProject up  where  up.user.id=?1 " )  
	// @EntityGraph(attributePaths = { "project","project.projectPics" })
	 Page<UserProject>  findOrders(Long userId, Pageable pageable);
	 
	 @Query( value = " select up from UserProject up  where  up.user.id=?1 and up.userProjectStatus=0  order by id desc" )  
		 List<UserProject>  findOrders(Long userId);
	 
	 @Query( value = " select up from UserProject up  where  up.user.id=?1 and up.project.id=?2 " )  
	// @EntityGraph(attributePaths = { "project","project.projectPics" })
	 Optional< UserProject>  findOneOrder(Long userId,Long projectId);
	 
	 Optional<Project> findById(Long id);
	 
	 
	 List<Project> findByUserId (Long userId);
	 
	 

}
